
<template>
  <div style="background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">充值订单管理</span>
    <el-button @click="beforeAddRechargeOrder" type="primary" style="float:right;margin-top:13px;">新建订单</el-button>
  </div>
  <!-- 新增订单弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input readonly :value="user.username" v-model="saveRechargeOrderForm.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">

        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="总金额">
            <el-input  v-model="saveRechargeOrderForm.amount"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单状态">

            <el-select placeholder="请选择" v-model="saveRechargeOrderForm.status">
              <el-option
                  label="测试1" :value="1"></el-option>

            </el-select>

          </el-form-item>
        </el-col>
      </el-row>


      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="支付方式">

            <el-select placeholder="请选择" v-model="saveRechargeOrderForm.paymentMethod">
              <el-option
                  label="测试1" :value="1"></el-option>

            </el-select>

          </el-form-item>
        </el-col>

      </el-row>

    </el-form>

    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveRechargeOrder">录入</el-button>
    </template>

  </el-dialog>


  <!-- 维修订单搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="订单号">
        <el-input placeholder="请输入订单号" style="width:220px;"
                  v-model="searchRechargeOrderForm.license" @keydown.enter="loadRechargeOrder"></el-input>
      </el-form-item>
      <el-form-item label="用户名" style="width:290px;">
        <el-input placeholder="请输入用户名" style="width:220px;"
                  v-model="searchRechargeOrderForm.username" @keydown.enter="loadRechargeOrder"></el-input>
      </el-form-item>



      <el-form-item label="订单状态" style="width: 290px;">
        <el-select placeholder="请选择订单状态" v-model="searchRechargeOrderForm.status" @change="loadRechargeOrder">
          <!--          <el-option v-for="i in orderTypeArr" label="i.label" value="i.value" ></el-option>-->

        </el-select>
      </el-form-item>



      <el-form-item>
        <el-button  @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadRechargeOrder">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 订单列表 -->
  <el-card style="margin:20px;">
    <el-table :data="RechargeOrderArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="orderId" label="订单ID" align="center"/>
      <el-table-column prop="userId" label="用户ID" align="center"/>
      <el-table-column prop="amount" label="总金额" align="center"/>
      <el-table-column prop="status" label="订单状态" align="center"
                       :formatter="rechargeOrderStatusTypeFormatter"/>
      <el-table-column prop="paymentMethod" label="支付方式" align="center"/>
      <el-table-column prop="updateTime" label="订单更新时间" align="center"/>
      <el-table-column prop="completeTime" label="订单完成时间" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editRechargeOrder(scope.row.orderId)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteRechargeOrder(scope.row.orderId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from '@/util/request';
import {ElMessage} from "element-plus";

const user = ref(getUser());
//控制新增订单弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('录入充值订单');
const searchRechargeOrderForm = ref({orderId:'',userId:'',status:''});
const RechargeOrderArr = ref([]);
//定义加载订单的方法
const loadRechargeOrder= ()=>{
  let data = qs.stringify(searchRechargeOrderForm.value);
  axios.get('/api/rechargeOrder/select?'+data).then((response)=>{
    if(response.data.code==2000){
      RechargeOrderArr.value = response.data.data;
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}

const resetSearch = ()=>{
  searchRechargeOrderForm.value = {};
  loadRechargeOrder();
}
//一进到页面立即执行调用加载订单的方法
onMounted(()=>{
  loadRechargeOrder();
})
//对表格中订单状态显示的文字进行转换




//定义对象用来保存新增订单弹窗表单的所有数据
const saveRechargeOrderForm = ref({
  username:'',
  orderId:'',
  userId:'',
  amount:'',
  status:'',
  paymentMethod:'',
});
const handleClose = ()=>{
  if (confirm('您确定要关闭弹窗吗')){
    dialogVisible.value = false;//关闭弹窗
    saveRechargeOrderForm.value = {};//清空表单数据
  }
}

//定义保存订单的方法
const saveRechargeOrder = ()=>{
  //对数据进行校验
  if(!saveRechargeOrderForm.value.paymentMethod){
    ElMessage.error('支付方式不能为空!');
    return;
  }
  let data = qs.stringify(saveRechargeOrderForm.value);
  axios.post('/api/rechargeOrder/create',data).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('保存成功!');
      //保存成功:关弹窗+清除表单数据+重新加载所有订单数据(新增数据才会显示)
      dialogVisible.value = false;
      saveRechargeOrderForm.value = {};
      loadRechargeOrder();
    }else{
      //数据库里的code和license字段设置了唯一,如果重复插入会操作失败,所以可以设置更确切的提示语句
      ElMessage.error('该订单正在维护，车牌号不允许重复插入!');
    }
  })
}
//
//定义点击"新建订单"按钮时调用的方法 出弹窗+改标题+清空表单数据
const beforeAddRechargeOrder = ()=>{
  dialogVisible.value = true;
  dialogTitle.value = '新建充值订单';
  saveRechargeOrderForm.value = {};
}
//定义编辑订单的方法 出弹窗+改标题+回显数据
const editRechargeOrder = (orderId)=>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑订单';
  //根据当前订单id回显弹窗表单中此订单的详细数据
  axios.get('/api/rechargeOrder/select?id='+orderId).then((response)=>{
    if (response.data.code==2000){
      saveRechargeOrderForm.value = response.data.data[0];
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//定义删除订单的方法
const deleteRechargeOrder = (orderId)=>{
  if(confirm('您确认要删除该订单吗?')){
    axios.post('/api/rechargeOrder/delete/'+orderId).then((response)=>{
      if(response.data.code==2000){
        ElMessage.success('删除成功!');
        //删除成功后,需要重新加载所有的订单数据列表!
        loadRechargeOrder();
      }else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}
const rechargeOrderStatusTypeFormatter = (row,column,cellValue,index)=>{
  //如果字典项数组没有值,直接返回当前单元格的值,比如 10
  // if (!materialTypeArr.value){
  //   return cellValue;
  // }
  // for (let  i of materialTypeArr.value){
  //   if (i.value == cellValue){
  //     cellValue = i.label;
  //   }
  // }
  return 1;
}
</script>